import { RbacPermissionType } from '@/datatypes/rbacPermission/data';
import { list } from '@/services';
import { rbacPermissionUrl } from '@/urls';
import { useEffect, useState } from 'react';

export const useAdminPermissions = () => {
  const [adminPermission, setAdminPermission] = useState<any>([]);

  useEffect(() => {
    const fetchRolesData = async () => {
      const data: API.ListResult<RbacPermissionType> = await list(rbacPermissionUrl, { type: 1 });
      const transformedData = data?.list?.map((item) => ({ name: item.name, key: item.id })) || [];

      setAdminPermission(transformedData || []);
    };

    fetchRolesData().then();
  }, []);

  return adminPermission;
};

export const useOpenPermissions = () => {
  const [openPermission, setOpenPermission] = useState<any>([]);

  useEffect(() => {
    const fetchRolesData = async () => {
      const data: API.ListResult<RbacPermissionType> = await list(rbacPermissionUrl, { type: 2 });
      const transformedData = data?.list?.map((item) => ({ name: item.name, key: item.id })) || [];

      setOpenPermission(transformedData || []);
    };

    fetchRolesData().then();
  }, []);

  return openPermission;
};
